package com.hzya.frame.utils.core.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

public final class WebContext {
	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
				.currentRequestAttributes();
		return attributes.getRequest();
	}

	public static Map<?, ?> getRequestParameterMap() {
		if (getRequest() != null)
			return getRequest().getParameterMap();

		return new HashMap();
	}

	public static Map getRequestAttributeMap() {
		Map map = new HashMap();
		if (getRequest() != null) {
			Enumeration enumeration = getRequest().getAttributeNames();
			while (enumeration.hasMoreElements()) {
				Object key = enumeration.nextElement();
				map.put(key, getRequest().getAttribute((String) key));
			}
		}
		return map;
	}

	public static HttpServletResponse getResponse() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
				.currentRequestAttributes();
		return attributes.getResponse();
	}

	public static HttpSession getSession() {
		HttpServletRequest request = getRequest();
		if (request != null)
			return request.getSession();

		return null;
	}

	public static Map getSessionMap() {
		Map map = new HashMap();
		if (getSession() != null) {
			Enumeration enumeration = getSession().getAttributeNames();
			while (enumeration.hasMoreElements()) {
				Object key = enumeration.nextElement();
				map.put(key, getSession().getAttribute((String) key));
			}
		}
		return map;
	}

	public static ServletContext getServletContext() {
		HttpSession actionSession = getSession();
		return actionSession.getServletContext();
	}

	public static Map getApplicationMap() {
		Map map = new HashMap();
		if (getServletContext() != null) {
			Enumeration enumeration = getServletContext().getAttributeNames();
			while (enumeration.hasMoreElements()) {
				Object key = enumeration.nextElement();
				map.put(key, getSession().getAttribute((String) key));
			}
		}
		return map;
	}

	public static String getContextPath() {
		if (getRequest() != null)
			return getRequest().getContextPath();

		return null;
	}

	public String getServletPath() {
		return getRequest().getServletPath().replaceFirst("/", "");
	}

	public String getReferer() {
		String referer = "";
		if (getRequest().getHeader("Referer") != null)
			referer = getRequest().getHeader("Referer");
		else
			referer = "";

		return referer;
	}

	public static String getUrl() {
		if (getRequest() != null) {
			String url = getRequest().getRequestURI();
			String contextpath = getRequest().getContextPath();
			if (url.startsWith(contextpath))
				url = url.substring(contextpath.length() + 1);

			String queryString = getRequest().getQueryString();
			if (queryString != null)
				url = url + "?" + queryString;

			return url;
		}
		return null;
	}

	public static Object getBean(String service_name) {
		WebApplicationContext webApplicationContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		return webApplicationContext.getBean(service_name);
	}

	public static String getRealPath(String logicPath) {
		if (getRequest() != null)
			return getRequest().getRealPath(logicPath);

		return null;
	}

	public String contextUrl() {
		String contextUrl = getRequest().getHeader("host") + "/" + getRequest().getContextPath();
		contextUrl = "http://" + contextUrl.replaceAll("//", "/");
		if (contextUrl.endsWith("/"))
			contextUrl = contextUrl.substring(0, contextUrl.length() - 1);

		return contextUrl;
	}

	public static String urlDecoder(String arg0) {
		try {
			return URLDecoder.decode(arg0, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String urlEncoder(String arg0) {
		if ((arg0 == null) || (arg0.equals("")))
			return "";
		try {
			return URLEncoder.encode(arg0, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
}